<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class BaseItemDescriptionBase extends BaseItemBase {
	
	//constructor
	function BaseItemDescriptionBase() {
		parent::BaseItemBase();
	}
	
	//methods
	function getItems($filter = '', $check = 0, $limit = true, $level = 1) {
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$query_category = 0;
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and a." . $this->prefix . "status = 1";
		}
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( is_array($this->filter_query) ) {
				$filter = array_merge($filter, $this->filter_query);
			}
			//
			if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
				$query_str .= " and ad." . $this->prefix . "subject like '%" . $filter['keyword'] . "%' ";
			}
			if( isset($filter['category']) && $this->_not_null($filter['category']) == true ) {
				$query_category = 1;
				if( is_array($filter['category']) ) {
					$query_str .= " and a." . $this->prefix . "id = a2c." . $this->prefix . "id and a2c.categories_id in (" . implode(', ', $filter['category']) . ") ";
				} else {
					$query_str .= " and a." . $this->prefix . "id = a2c." . $this->prefix . "id and a2c.categories_id = '" . (int)$filter['category'] . "' ";
				}
			}
			if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
				if( is_array($filter['ids']) ) {
					$query_str .= " and a." . $this->prefix . "id in (" . implode(', ', $filter['ids']) . ") ";
				} else {
					$query_str .= " and a." . $this->prefix . "id = '" . (int)$filter['ids'] . "' ";
				}
			}
		}
		if( $level == 0 ) {
			if( $query_category == 1 ) {
				$query = "select a.*, ad." . $this->prefix . "subject from " . $this->table . " a, " . $this->table_description . " ad, " . $this->table_to_category . " a2c where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str . $query_limit_str;
			} else {
				$query = "select a.*, ad." . $this->prefix . "subject from " . $this->table . " a, " . $this->table_description . " ad where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str . $query_limit_str;
			}
		} else {
			if( $query_category == 1 ) {
				$query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad, " . $this->table_to_category . " a2c where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str . $query_limit_str;
			} else {
				$query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str . $query_limit_str;
			}
		}
		
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			$rows[] = $row;
		}
		
		return $rows;
	}
	
	function getItemsTotal($filter = '', $check = 0, $limit = true) {
		$query_category = 0;
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and a." . $this->prefix . "status = 1";
		}
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( is_array($this->filter_query) ) {
				$filter = array_merge($filter, $this->filter_query);
			}
			//
			if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
				$query_str .= " and ad." . $this->prefix . "subject like '%" . $filter['keyword'] . "%' ";
			}
			if( isset($filter['category']) && $this->_not_null($filter['category']) == true ) {
				$query_category = 1;
				if( is_array($filter['category']) ) {
					$query_str .= " and a." . $this->prefix . "id = a2c." . $this->prefix . "id and a2c.categories_id in (" . implode(', ', $filter['category']) . ") ";
				} else {
					$query_str .= " and a." . $this->prefix . "id = a2c." . $this->prefix . "id and a2c.categories_id = '" . (int)$filter['category'] . "' ";
				}
			}
		}
		

		if( $query_category == 1 ) {
			$query = "select distinct count(*) as count from " . $this->table . " a, " . $this->table_description . " ad, " . $this->table_to_category . " a2c where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str;
		} else {
			$query = "select distinct count(*) as count from " . $this->table . " a, " . $this->table_description . " ad where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str;
		}
		
		$row = $this->adb->getRow($query);
		
		return $row['count'];
	}
	
	function getLatestItems($limit_length = 5) {
		if( $limit_length > 0 ) {
			
		} else {
			$limit_lenght = 5;
		}
		$query_limit_str = " limit " . $limit_length;
		$query = "select a.*, ad." . $this->prefix . "subject from " . $this->table . " a, " . $this->table_description . " ad where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' order by a." . $this->prefix . "date_added desc " . $query_limit_str;
		
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			$rows[] = $row;
		}
		
		return $rows;
	}	
	
	//
	function addItem($data_array) {
		$data_array = func_db_input($data_array);
		
		$data_array[$this->prefix . 'owner'] = $this->owner;
		$data_array[$this->prefix . 'date_added'] = 'now()';
		$data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
		$data_array[$this->prefix . 'status'] = '1';
		
		$items_id = $this->_insert($this->table, $data_array);
		
		//description
		$data_array[$this->prefix . 'id'] = $items_id;
		$data_array['languages_id'] = $this->languages_id;
		
		$this->_add($this->table_description, $data_array);
		
		//link
		if( $items_id > 0 ) {
			$this->_generate_item_link($items_id, $data_array[$this->prefix . 'subject']);
		}
		
		//category
		$categories_id = $data_array['categories_id'];
		$query = "insert into " . $this->table_to_category . " (" . $this->prefix . "id, categories_id) values ('" . $items_id . "', '" . $categories_id . "')";
		$result = $this->adb->query($query);
		
		return true;
	}
	
	function addItemCategory($items_id, $categories_id) {
		if( $items_id > 0 && $categories_id > 0 ) {
			$query = "select count(*) as count from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' and categories_id = '" . (int)$categories_id . "'";
			$count = $this->adb->getCount($query);
			if( $count == 0 ) {
				$query = "insert into " . $this->table_to_category . " (" . $this->prefix . "id, categories_id) values ('" . (int)$items_id . "', '" . (int)$categories_id . "')";
				$result = $this->adb->query($query);
				
				return true;
			} elseif( $nums == 1 ) {
				return 1;
			} elseif( $nums > 1 ) {
				return 2;
			}
		}
		
		return false;
	}
	
	function addItemView($items_id) {
		$query = "update " . $this->table . " set " . $this->prefix . "viewed = " . $this->prefix . "viewed + 1 where " . $this->prefix . "id = '" . (int)$items_id . "'";
		$result = $this->adb->query($query);
		
		return true;
	}
	
	function getItem($value, $type = 'id', $check = 0) {
		$value = func_db_input($value);
		
		if( $check == -1 ) {
			$query_str = "";
		} else {
			$query_str = " and a." . $this->prefix . "status = 1";
		}
		if( $type == 'id' ) {
			$query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad " 
					. " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = '" . (int)$value . "' " 
					. " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str;
		} elseif( $type == 'link' ) {
			$query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad " 
					. " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "link = '" . $value . "' " 
					. " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str;
		} else {
			return false;
		}
		
		return $this->adb->getRow($query);
	}
	
	function getPreviousItem($value, $type = 'id', $check = 0) {
		$value = func_db_input($value);
		
		$query_limit_str = " limit 1";
		if( $check == -1 ) {
			$query_str = "";
		} else {
			$query_str = " and a." . $this->prefix . "status = 1";
		}
		$query_str .= " order by " . $this->prefix . "id desc ";
		if( $type == 'link' ) {
			$item = $this->getItem($value, 'link', -1, 0);
			$value = $item[$this->prefix . 'id'];
		}
		$query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad " 
				. " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id < '" . (int)$value . "' " 
				. " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " 
				. $query_str . $query_limit_str;
		
		return $this->adb->getRow($query);
	}
	
	function getNextItem($value, $type = 'id', $check = 0) {
		$value = func_db_input($value);
		
		$query_limit_str = " limit 1";
		if( $check == -1 ) {
			$query_str = "";
		} else {
			$query_str = " and a." . $this->prefix . "status = 1";
		}
		$query_str .= " order by " . $this->prefix . "id asc ";
		if( $type == 'link' ) {
			$item = $this->getItem($value, 'link', -1, 0);
			$value = $item[$this->prefix . 'id'];
		}
		$query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad " 
				. " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id > '" . (int)$value . "' " 
				. " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " 
				. $query_str . $query_limit_str;
		
		return $this->adb->getRow($query);
	}
	
	function getCategoryID($items_id) {
		$query = "select categories_id from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
		$row = $this->adb->getRow($query);
		
		return $row['categories_id'];
	}
	
	function getCategory($items_id) {
		$query = "select c.*, cd.categories_name from " . $this->prefix . "categories c, " . $this->prefix . "categories_description cd, " . $this->table_to_category . " a2c where c.categories_id = cd.categories_id and cd.languages_id = '" . (int)$this->languages_id . "' and c.categories_id = a2c.categories_id and a2c." . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
		
		return $this->adb->getRow($query);
	}
	
	function getCategories($items_id) {
		$query = "select c.*, cd.categories_name from " . $this->prefix . "categories c, " . $this->prefix . "categories_description cd, " . $this->table_to_category . " a2c where c.categories_id = cd.categories_id and cd.languages_id = '" . (int)$this->languages_id . "' and c.categories_id = a2c.categories_id and a2c." . $this->prefix . "id = '" . (int)$items_id . "'";
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			$rows[] = $row;
		}
		
		return $row;
	}
	
	function getCategoriesID($items_id) {
		$query = "select categories_id from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			$rows[] = $row['categories_id'];
		}
		
		return $rows;
	}
	
	function updateItem($items_id, $data_array) {
		$data_array = func_db_input($data_array);
		
		$i = 0;
		$_fields = array($this->prefix . 'owner', $this->prefix . 'link', $this->prefix . 'type', $this->prefix . 'start_time', $this->prefix . 'end_time', $this->prefix . 'sort_order', $this->prefix . 'status');
		$query = "update " . $this->table . " set ";
		foreach($data_array as $k => $v) {
			if( in_array($k, $_fields) ) {
				if( $i == 0 ) {
					$query .= " " . $k . " = '" . $v . "' ";
				} else {
					$query .= " ," . $k . " = '" . $v . "' ";
				}
				$i++;
			}
		}
		if( $i > 0 ) {
			$query .= ", " . $this->prefix . "last_modified = now(), " . $this->prefix . "modified_by = '' where " . $this->prefix . "id = '" . (int)$items_id . "'";
			$result = $this->adb->query($query);
		}
		//desc
		$i = 0;
		reset($data_array);
		$_description_fields = array($this->prefix . 'subject', $this->prefix . 'subject2', $this->prefix . 'keywords', $this->prefix . 'introtext', $this->prefix . 'fulltext');
		$query = "update " . $this->table_description . " ad set ";
		foreach($data_array as $k => $v) {
			if( in_array($k, $_description_fields) ) {
				if( $i == 0 ) {
					$query .= " " . $k . " = '" . $v . "' ";
				} else {
					$query .= ", " . $k . " = '" . $v . "' ";
				}
				$i++;
			}
		}
		if( $i > 0 ) {
			$query .= " where " . $this->prefix . "id = '" . (int)$items_id . "' and languages_id = '" . (int)$this->languages_id . "'";
			$result = $this->adb->query($query);
		}
		
		return true;
	}
	
	function updateLink($items_id, $items_link) {
		$items_link = func_db_input($items_link);
		
		$query = "select count(*) as count from " . $this->table . " where " . $this->prefix . "id != '" . $items_id . "' and " . $this->prefix . "link = '" . $items_link . "'";
		$row = $this->adb->getRow($query);
		if( $row['count'] > 0 ) {
			return -1;
		} else {
			$query = "update " . $this->table . " set " . $this->prefix . "link = '" . $items_link . "' where " . $this->prefix . "id = '" . (int)$items_id . "'";
			$result = $this->adb->query($query);
		}
		
		return 1;
	}
	
	function deleteItem($items_id) {
		$query = "delete " . $this->table . ", " . $this->table_description . " from " . $this->table . " a, " . $this->table_description . " ad " 
				. " where a." . $this->prefix . "id = '" . (int)$items_id . "' " 
				. " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "'";
		
		return $this->adb->delete($query);
	}
	
	//private
	function _generate_item_link($items_id, $items_name = '', $items_link = '') {
		if( $items_link == '' && $items_name != '' ) {
			$items_link = $this->_filter_link($items_name);
		}
		if( $this->_not_null($items_link) ) {
			$items_link = func_db_input($items_link);
			$query = "select count(*) as count from " . $this->table . " where " . $this->prefix . "id != '" . (int)$items_id . "' " 
					. " and " . $this->prefix . "link = '" . $items_link . "'";
			$count = $this->adb->getCount($query);
			if( $count == 0 ) {
				$this->updateItem($items_id, array($this->prefix . 'link' => $items_link));
				return true;
			}
		}
		$items_link = $this->_filter_link($items_name);
		$items_link = $this->_rand_link($items_id, $items_link);
		
		$this->_generate_item_link($items_id, $items_name, $items_link);
		
		return true;
	}
	
	function _generate_item_link_lang($items_id) {
		$item = $this->getItem($items_id);
		$item_link = $item[$this->prefix . 'link'];
		
		if( $this->_not_null($item_link) ) {
			$query = "select count(*) as count from " . $this->table . " a, " . $this->table_description . " ad " 
					. " where a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " 
					. " and a." . $this->prefix . "id != '" . $items_id . "' and a." . $this->prefix . "link = '" . $item_link . "'";
			$count = $this->adb->getCount($query);
			if( $count == 0 ) {
				$this->updateItem($items_id, array($this->prefix . 'link' => $items_link));
				return false;
			}
		}
		$items_link = $this->_filter_link($item[$this->prefix . 'subject']);
		$items_link = $this->_rand_link($items_id, $items_link);
		
		$this->updateItem($items_id, array($this->prefix . 'link' => $items_link));
		$this->_generate_item_link($items_id);
		
		return true;
	}
}
//
?>